package club.hicode.daydayup.lambda.learn;

import cn.hutool.core.util.RandomUtil;
import com.github.sd4324530.jtuple.Tuple2;
import com.github.sd4324530.jtuple.Tuple3;
import org.junit.Test;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * 测试
 *
 * @author Liu Chunfu
 * @date 2018-05-04 下午3:37
 **/
public class LambdaTest {

    /**
     * 菜单信息
     */
    private List<Dish> menu = Dish.MENU;

    //private int num = 300;

    /**
     * 测试1
     */
    @Test
    public void test1() {
        int num = 300;
        List<String> names = menu.stream()
                .filter(d -> check(d)) //过滤
                .map(Dish::getName) //映射
                .collect(Collectors.toList()); // 组合

    }

    private boolean check(Dish dish) {
        //xxx
        //xx
        //rer
        return true;
    }

    /**
     * 映射<br/>
     * 提取菜单中菜的名称
     */
    @Test
    public void test2_1() {
        List<String> menuNameList = new ArrayList<>();
        for (Dish dish : menu) {
            menuNameList.add(dish.getName());
        }
    }

    /**
     * 映射<br/>
     * 提取菜单中菜的名称
     */
    @Test
    public void test2_2() {
        List<String> menuNameList = menu.stream().map(m -> m.getName()).collect(Collectors.toList());
    }

    /**
     * 映射<br/>
     * 提取菜单中菜的卡路里
     */
    @Test
    public void test2_3() {
        Stream<Dish> dishStream = menu.stream().filter(dish -> dish.getName().length() > 3);
        menu.sort(Comparator.comparing(Dish::getCalories));
        List<Integer> calarList = menu.stream().map(m -> m.getCalories()).collect(Collectors.toList());
    }

    /**
     * 映射<br/>
     * 提取菜单中菜的卡路里
     */
    @Test
    public void test2_4() {

        List<Tuple3<String, Dish.Type, Integer>> collect = menu.stream().map(m -> Tuple3.with(m.getName(), m.getType(), m.getCalories()))
                .collect(Collectors.toList());

    }


    //就是把方法当做参数传到stream内部

    /**
     * 匹配：boolen的 anymatch,allmatch
     */
    @Test
    public void test3_1() {
        boolean b = menu.parallelStream().allMatch(Dish::isVegetarian);
        System.out.println(b);
    }

    /**
     * 查找 findFirst findAny
     */
    @Test
    public void test4_1() {

        List<Dish> collect = menu.parallelStream().filter(m -> {
            System.out.println("---");
            return m.getCalories() < 200;
        }).filter(m -> {
            System.out.println("xxxx");
            return true;
        }).collect(Collectors.toList());

    }

    /**
     * 流可以用在哪些地方？
     */
    @Test
    public void test5_1() {
        //    list
        //    array
        Stream<Dish> stream = Arrays.stream(Dish.MENU2);

    }

    /**
     * 生成流
     */
    @Test
    public void test6_1() {
        for (int i = 0; i < 2; i++) {
            System.out.println(i);
        }
        System.out.println("----------");
        Stream<Integer> limit = Stream.iterate(0, n -> n + 1).limit(2);
        limit.forEach(System.out::println);
    }

    /**
     * 生成流
     */
    @Test
    public void test6_2() {
        Stream<Integer> limit = Stream.generate(RandomUtil::randomInt).limit(5);
    }

    /**
     * 分组
     */
    @Test
    public void groupTest() {
        Map<Dish.Type, List<Dish>> collect =
                menu.stream().collect(Collectors.groupingBy(Dish::getType));
    }
}
